package com.ecarx.vhaltests.hy11.testcases.charging;

import static com.ecarx.xui.adaptapi.car.property.IProperty.CHARGE_FUNC_DC_CHARGING;

import android.car.VehicleAreaType;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.property.IPropertyValue;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ChargeFuncDCChargingTest extends VhalUnitTestBase {
    private static int propertyId;
    private static IWrapper.IPropertyId wrapperProperty;
    private static IpcpClient ipcpClient = new IpcpClient();
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    @BeforeClass
    public static void testInit() {
        IWrapper wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        wrapperProperty = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, CHARGE_FUNC_DC_CHARGING);
        propertyId = wrapperProperty.getPropertyId();
        setAvailable(true);
        Assert.assertTrue(isAvail());
    }

    @Test
    public void test001_SetChargeFuncDCChargingStop() {
        testSetGet(IPropertyValue.ChargingSwitchState.STOP);
    }

    @Test
    public void test002_SetChargeFuncDCChargingInvaildParam() {
        setValue(IPropertyValue.ChargingSwitchState.STOP);
        setValue(-1);
        Assert.assertEquals(wrapperProperty.getPropertyValue(IPropertyValue.ChargingSwitchState.STOP).intValue(), getValue());
    }

    @Test
    public void test003_SetChargeFuncDCChargingUnAvail() {
        setAvailable(false);
        Assert.assertFalse(isAvail());
    }

    private static void setAvailable(boolean isAvail) {
        int ccSvcId = 135;
        int[] ccOperIds = {200};

        int cemSvcId = 37;
        int[] cemOperIds = {999};

        int vddmSvcId = 48;
        int[] vddmOperIds = {999};

        ipcpClient.setConfig(ccSvcId, ccOperIds);
        ipcpClient.setConfig(cemSvcId, cemOperIds);
        ipcpClient.setConfig(vddmSvcId, vddmOperIds);

        int[] ccConfigs = {13, 573};
        int[] ccValues = {4, 2};
        boolean ccRet = ipcpClient.setCarconfigSignal(ccSvcId, ccOperIds[0], ccConfigs, ccValues);
        Assert.assertTrue(ccRet);

        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {isAvail ? VehicleConstants.CAR_MODE_NOMAL : VehicleConstants.CAR_MODE_TRANSPORT, VehicleConstants.USG_MODE_CONVENIENCE};
        boolean cemRet = ipcpClient.setCEMSignal(cemSvcId, cemOperIds[0], cems, cemValues);
        Assert.assertTrue(cemRet);

        String[] vddmConfigs = {"chrgnOrDisChrgnStsFb", "dCChrgnHndlSts", "onBdChrgrHndlSts1"};
        int[] vddmValues = {15, 2, 8};
        boolean retVDDM = ipcpClient.setVDDMSignal(vddmSvcId, vddmOperIds[0], vddmConfigs, vddmValues);
        Assert.assertTrue(retVDDM);
    }

    private void testSetGet(int value) {
        setValue(value);
        Assert.assertEquals(wrapperProperty.getPropertyValue(value).intValue(), getValue());
    }

    private void setValue(int value) {
        setAvailable(true);
        delay();
        mCarPropertyMgr.setIntProperty(propertyId, AREA_ID, wrapperProperty.getPropertyValue(value));
    }

    private int getValue() {
        delay();
        return mCarPropertyMgr.getIntProperty(propertyId, AREA_ID);
    }

    private static boolean isAvail() {
        delay();
        return mCarPropertyMgr.isPropertyAvailable(propertyId, AREA_ID);
    }

}